package com.west_traveler.tiyutoutiao.fragment.news_viewpager_fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.west_traveler.tiyutoutiao.R;
import com.west_traveler.tiyutoutiao.adapter.CommentRecyclerAdapter;
import com.west_traveler.tiyutoutiao.application.BaseApplication;
import com.west_traveler.tiyutoutiao.databinding.FragmentNewsViewpagerBinding;
import com.west_traveler.tiyutoutiao.entity.recommend_entity.BannersBean;
import com.west_traveler.tiyutoutiao.entity.recommend_entity.ContentsBean;
import com.west_traveler.tiyutoutiao.entity.recommend_entity.ContestListBean;
import com.west_traveler.tiyutoutiao.entity.recommend_entity.Recommend;
import com.west_traveler.tiyutoutiao.entity.recommend_entity.TopsBean;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsViewPagerFragment extends Fragment implements Callback<Recommend> {
    private FragmentNewsViewpagerBinding binding;
    private CommentRecyclerAdapter<Object> adapter;

    public static NewsViewPagerFragment getInstance(int channel_id, String data_version) {
        NewsViewPagerFragment fragment = new NewsViewPagerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("channel_id", channel_id);
        bundle.putString("data_version", data_version);
        fragment.setArguments(bundle);
        return fragment;
    }

    public NewsViewPagerFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(container.getContext()), R.layout.fragment_news_viewpager, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle bundle = getArguments();
        int channel_id = bundle.getInt("channel_id");
        String data_version = bundle.getString("data_version");
        HashMap<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        //  map.put(DataBean.class, Pair.create(R.layout.banners, com.west_traveler.tiyutoutiao.BR.banners));
        map.put(ContentsBean.class, Pair.create(R.layout.contents_item, com.west_traveler.tiyutoutiao.BR.content));
        map.put(TopsBean.class, Pair.create(R.layout.tops_item, com.west_traveler.tiyutoutiao.BR.tops));
        adapter = new CommentRecyclerAdapter<>(getContext(), new LinkedList<>(), map);
        binding.rvNews.setAdapter(adapter);
        //((BaseApplication) getActivity().getApplication()).getService().getData("0/contest_flag=true/data_version=397.3/banner_flag=true/machine_code=865682026799303&client=key%3ATiyuForAndroid/version=2.11/format=json/app_id=4/is_emulator=false").enqueue(this);
        ((BaseApplication) getActivity().getApplication()).getService().getData(channel_id, true, Double.parseDouble(data_version), "true&machine_code=865682026799303&client=key%3ATiyuForAndroid/version=2.11&format=json&app_id=4&is_emulator=false").enqueue(this);
    }

    @Override
    public void onResponse(Call<Recommend> call, Response<Recommend> response) {
        Recommend body = response.body();
        List<BannersBean> banners = response.body().getData().getBanners();
        List<ContentsBean> contents = response.body().getData().getContents();
        List<TopsBean> tops = response.body().getData().getTops();
        List<ContestListBean> contest_list = response.body().getData().getContest_list();

        //adapter.addAll(0, contest_list);
        if (tops == null) {
            //adapter.addAll(0, tops);
            adapter.addAll(0, contents);
        } else {
            // adapter.addAll(0, tops);
            adapter.addAll(0, contents);
        }
        //adapter.addAll(1, banners);
    }

    @Override
    public void onFailure(Call<Recommend> call, Throwable t) {
        t.getStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
